package com.ytc.pquery.service.Impl;

import com.ytc.data.mapper.DataMapper;
import com.ytc.data.model.Data;
import com.ytc.data.service.DataService;
import com.ytc.position.model.Position;
import com.ytc.pquery.mapper.AddToLikeMapper;
import com.ytc.pquery.mapper.PqueryMapper;
import com.ytc.pquery.model.AddToLike;
import com.ytc.pquery.model.PositionDetails;
import com.ytc.pquery.model.Pquery;
import com.ytc.pquery.service.PqueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PqueryServiceImpl implements PqueryService {

    @Autowired
    private PqueryMapper pqueryMapper;

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private AddToLikeMapper addToLikeMapper;

    @Override
    public List<PositionDetails> positionList(String dataIds,String positionName){
        //查询条件类
        Pquery pquery = new Pquery();

        String[] ids = dataIds.split(",");

        List<Data> dataList = dataMapper.getDataList(ids);//条件集合

        if (dataList.size() > 0) {
            for (Data data:dataList){
                if(data.getDataRole().equals("公司类型")){
                    pquery.setCompanyType(data.getDataName());
                } else if (data.getDataRole().equals("公司规模")) {
                    pquery.setCompanySize(data.getDataName());
                } else if (data.getDataRole().equals("融资阶段")) {
                    pquery.setFinancingPhase(data.getDataName());
                } else if (data.getDataRole().equals("学历要求")) {
                    pquery.setDegree(data.getDataName());
                } else if (data.getDataRole().equals("工作经验")) {
                    pquery.setExperience(data.getDataName());
                } else if (data.getDataRole().equals("薪资待遇")) {
                    pquery.setSalary(data.getDataName());
                }
            }
        }

        pquery.setSearch(positionName);//搜索框

        return pqueryMapper.positionList(pquery);
    }

    @Override
    public List<PositionDetails> positionLike(Integer userId){
        return pqueryMapper.positionLike(userId);
    }


    @Override
    public PositionDetails getPqueryDetails(Integer positionId){
        return pqueryMapper.getPqueryDetails(positionId);
    }

    //收藏
    @Override
    public void addToLike(Integer positionId,Integer userId){
        //获取数据，判断是否收藏
        AddToLike addToLike = addToLikeMapper.getAddToLike(positionId, userId);

        if (null != addToLike && !"".equals(addToLike)) { //有记录
            if (addToLike.getStatus()==1) { //已收藏
                addToLike.setStatus(0);
            } else { //曾经取消收藏
                addToLike.setStatus(1);
            }
            addToLikeMapper.updateToLike(addToLike);//有记录只要修改即可
        } else {
            AddToLike addToLike1 = new AddToLike();
            addToLike1.setPositionId(positionId);
            addToLike1.setUserId(userId);
            addToLike1.setStatus(1);
            addToLikeMapper.addToLike(addToLike1);//没有记录需要新增
        }
    }


    @Override
    public AddToLike getAddToLike(Integer positionId,Integer userId){
        return addToLikeMapper.getAddToLike(positionId, userId);
    }

}
